package pl.diagnoser.server.persistance.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import pl.diagnoser.client.dto.ObjectsDTO;
import pl.diagnoser.server.persistance.map.App;
import pl.diagnoser.server.persistance.map.ObjectAttributes;
import pl.diagnoser.server.persistance.map.Objects;
import pl.diagnoser.server.persistance.tools.HibernateUtil;

public class ObjectsDao implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings("unchecked")
	public List<ObjectsDTO> getListOfAllObjects() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		App app = new AppDao().getActiveApp();
		List<Objects> objectList = session.createCriteria(Objects.class).add(Restrictions.eq("app", app)).list();
		//List<Objects> objectList = new ArrayList<Objects>(session.createQuery("from Objects").list());
		List<ObjectsDTO> objectListDTO = new ArrayList<ObjectsDTO>(objectList != null ? objectList.size() : 0);
		if(objectList != null) {
			for (Objects objects : objectList) {
				objectListDTO.add(createObjectsDTO(objects));
			}
		}
		session.getTransaction().commit();
		session.close();
		return objectListDTO;
	}
	
	public Objects getObject(long objectId) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Objects objects = (Objects) session.createCriteria(Objects.class).add(Restrictions.eq("id", objectId)).uniqueResult();
		session.getTransaction().commit();
		session.close();
		
		return objects;
	}
	
	public Objects getObjectByMd5(String nameMd5) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Objects objects = (Objects) session.createCriteria(Objects.class).add(Restrictions.eq("nameMd5", nameMd5)).uniqueResult();
		session.getTransaction().commit();
		session.close();
		return objects;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Long saveNewObject(String objectName, String objectDate, HashMap<String, String> objectAttributes, HashMap<String, List<String>> objectMultipleAttributes) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		
		Objects objects = new Objects(objectName, objectDate, new AppDao().getActiveApp());
		Long generatedId = (Long) session.save(objects);
		
		Iterator<Entry<String, String>> it = objectAttributes.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry pairs = (Map.Entry)it.next();
			if((String)pairs.getKey() != null)
				session.save(new ObjectAttributes(objects,(String)pairs.getKey(), (String)pairs.getValue()));
			it.remove();
		}
		
		Iterator<Entry<String, List<String>>> it2 = objectMultipleAttributes.entrySet().iterator();
		while(it2.hasNext()) {
			Map.Entry pairs = (Map.Entry) it2.next();
			for(String objectMultipleAttributess : (List<String>)pairs.getValue()) {
				if(objectMultipleAttributess != null)
					session.save(new ObjectAttributes(objects,(String)pairs.getKey(),objectMultipleAttributess));
			}
		}
		
		session.getTransaction().commit();
		session.close();
		return generatedId;
	}
	
	public ObjectsDTO createObjectsDTO(Objects objects) {
		return new ObjectsDTO(objects.getName(), objects.getId());
	}
}
